package example;

import helpers.UrlHelper;

import java.io.IOException;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class RelativeLinkEx extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String url = "http://www.android.com/devices/detail/nexus-7";
		
		try {
			Document doc = Jsoup.connect(url).get();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}
	
	public static void main(String[] args) {
		String url = "http://www.android.com/devices/detail/nexus-7";
		
		try {
			Document doc = Jsoup.connect(url).get();
			
			Elements imgs = doc.select(".g-unit .detail .g-section").select("img");
			for (int i = 0; i < imgs.size(); i++) {
				String src = imgs.get(i).attr("src");
				if(!Pattern.matches("^(http|https)://[\\w\\W]+$",src))
				{
					src = "http://"+UrlHelper.fomatDmains("http://www.android.com/devices/") + src;
				}
				System.out.println(src);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
